package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.base.domain.Buy;
import cn.wolfcode.trip.base.query.BuyQueryObject;
import cn.wolfcode.trip.base.service.IBuyService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/buys")
public class BuyController {
    @Autowired
    private IBuyService buyService;
    @GetMapping("/{status}")
    public Object types(BuyQueryObject qo){
        return buyService.query(qo);
    }
    @GetMapping("/{id}/deletes")
    public Object delete(@PathVariable Long id){
        JSONResult result = new JSONResult();
        try {
            buyService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    @GetMapping("/{id}/address")
    public Object getAddress(@PathVariable Long id){
        return buyService.getAddress(id);
    }

    @PostMapping
    public Object saveOrUpdate(Buy buy) {
        JSONResult result = new JSONResult();
        try {
            buyService.saveOrUpdate(buy);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    @PostMapping("/{commodity.id}/{user.id}/{status}")
    public Object saveShop(Buy buy) {
        JSONResult result = new JSONResult();
        try {
            buyService.saveShop(buy);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    @GetMapping("/{userId}/commoditys")
    public Object getOrders(BuyQueryObject qo){
        return buyService.query(qo);
    }
    /**
     * 查询一条数据
     */
    @GetMapping("/{id}/commodity")
    public Object get(@PathVariable Long id){
        return buyService.get(id);
    }

    /**
     * 查询用户收藏的数据
     * @param qo
     * @return
     */
    @GetMapping("/{user.id}/collects")
    public Object get(BuyQueryObject qo){
        return buyService.query(qo);
    }
    @GetMapping("/{status}/prices")
    public Object prices(@PathVariable int status){
        List<Integer> nums= buyService.prices(status);
        Map<String, Object> map = new HashMap<String, Object>();
        int price=0;
        for (Integer num : nums) {
            price += num;
        }
        map.put("price",price);
        return map;
    }
}
